﻿using CKS2320.Common.Model;
using CKS2320.Common.Utils;
using CKS2320.Domain.DBEntity;
using CKS2320.Repository.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS2320.Repository
{
    public class EquipmentfailureRepository : IEquipmentfailureRepository
    {
        public DBContext _dbContext;
        public EquipmentfailureRepository(DBContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 获取故障码表
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public IEnumerable<Equipmentfailure> GetEquipmentfailures(string ip, string Language = "CN")
        {
            var a= _dbContext.Equipmentfailures.Where(w=>w.Plcip.Equals(ip)).ToList();
            return a;
            //if (!string.IsNullOrEmpty(ip))
            //{
            //    query = query.Where(p => p.Plcip == ip);
            //}
            //return query.ToList();
        }

        /// <summary>
        /// 根据ID获取一条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Equipmentfailurehistory? GetById(string Id)
        {
            try
            {
                Guid guid = Guid.Parse(Id);
                var entity = _dbContext.Equipmentfailurehistories.FirstOrDefault(p => p.Id == guid);
                return entity;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ResultReturn Edit(Equipmentfailurehistory entity)
        {
            try
            {
                _dbContext.Equipmentfailurehistories.Update(entity);
                int count = _dbContext.SaveChanges();
                if (count > 0)
                {
                    return MessageInfoUtils.Success(entity.Id);
                }
                return MessageInfoUtils.Success();
            }
            catch (Exception ex)
            {
                return MessageInfoUtils.Fail(ex.Message);
            }
        }

        /// <summary>
        /// 插入一条故障记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultReturn InsertHistory(Equipmentfailurehistory entity)
        {
            try
            {
                _dbContext.Equipmentfailurehistories.Add(entity);
                int result = _dbContext.SaveChanges();
                if (result > 0)
                {
                    return MessageInfoUtils.Success(entity.Id);
                }
            }
            catch (Exception ex)
            {
                return MessageInfoUtils.Fail(ex.Message);
            }
            return MessageInfoUtils.Fail();
        }

        /// <summary>
        /// 获取历史故障记录
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public IEnumerable<EquipmentfailurehistoryModel> GetEquipmentfailureHistory(DateTime historyDate, string Language)
        {
            var query1 = from a in _dbContext.Equipmentfailurehistories
                         join b in _dbContext.Equipmentfailures on a.FailureId equals b.Id into val2Grp
                         from grp in val2Grp.DefaultIfEmpty()
                         where a.CreatedTime >= historyDate && grp.Language.ToLower() == Language.ToLower() && a.FaultType == false
                         select new EquipmentfailurehistoryModel
                         {
                             Id = a.Id,
                             Content = a.Content,
                             FailureId = a.FailureId,
                             StartTime = a.StartTime,
                             EndTime = a.EndTime,
                             FaultType = a.FaultType,
                             TotalTime = a.TotalTime,
                             equipmentFailureInfo = new EquipmentfailureModel()
                             {
                                 Id = grp.Id,
                                 FailureIndex = grp.FailureIndex,
                                 FailureOffset = grp.FailureOffset,
                                 Language = grp.Language,
                                 Plcip = grp.Plcip
                             }
                         };
            return query1.ToList();
        }

        /// <summary>
        /// 获取正在故障阶段的记录
        /// </summary>
        /// <param name="Language"></param>
        /// <returns></returns>
        public IEnumerable<EquipmentfailurehistoryModel> GetFailureHistory(string ip, string Language)
        {
            var query1 = from a in _dbContext.Equipmentfailurehistories
                         join b in _dbContext.Equipmentfailures on a.FailureId equals b.Id into val2Grp
                         from grp in val2Grp.DefaultIfEmpty()
                         where a.EndTime == null && a.FaultType == true && grp.Plcip == ip && grp.Language.ToLower() == Language.ToLower()
                         select new EquipmentfailurehistoryModel
                         {
                             Id = a.Id,
                             Content = a.Content,
                             FailureId = a.FailureId,
                             StartTime = a.StartTime,
                             EndTime = a.EndTime,
                             FaultType = a.FaultType,
                             TotalTime = a.TotalTime,
                             equipmentFailureInfo = new EquipmentfailureModel()
                             {
                                 Id = grp.Id,
                                 FailureIndex = grp.FailureIndex,
                                 FailureOffset = grp.FailureOffset,
                                 Language = grp.Language,
                                 Plcip = grp.Plcip
                             }
                         };
            return query1.ToList();
        }

        public Equipmentfailure? GetIdByFailureId(Guid? id)
        {
           return _dbContext.Equipmentfailures.FirstOrDefault(f=>f.Id.Equals(id));
        }
    }
}
